package com.test.chapter2;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;

public class MaxTemperatureMapper extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> 
{

	private static final int MISSING = 9999;
	
	//按照hadoop权威指南里面说的,当前版本hadoop-1.1.2的api属于老版本,使用老的api,新的api不是这个样子
	@Override
	public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter)
			throws IOException 
	{
		String line = value.toString();
		
		String year = line.substring(15, 19);
		
		int airTemperature;
		if(line.charAt(87) == '+')
		{
			airTemperature = Integer.parseInt(line.substring(88, 92));
		}
		else
		{
			airTemperature = Integer.parseInt(line.substring(87, 92));
		}
		
		String quality = line.substring(92, 93);
		/*
		 * quality.matchs("[01459]")的意思是quality这个字符是否能匹配上0,1,4,5,9这几个字符中其中的一个，返回值是ture或者false。
		   [a,b,c,d...]是正则表达式里一个集合的意思
		 * */
		if(airTemperature != MISSING && quality.matches("[01459]"))
		{
			output.collect(new Text(year), new IntWritable(airTemperature));
		}		
	}
	
	
	
	
}